package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutContract;
import com.lkd.contract.VendoutData;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-12-21 11:48
 */
//出货消息处理类
@Component
@Topic(TopicConfig.VMS_VENDOUT_TOPIC)
@Slf4j
public class VendoutHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {

        //1.校验参数
        VendoutContract vendoutContract = JsonUtil.getByJson(jsonMsg, VendoutContract.class);
        if(vendoutContract == null || vendoutContract.getVendoutData() == null ||
                StringUtils.isEmpty(vendoutContract.getVendoutData().getOrderNo())
            || vendoutContract.getVendoutData().getSkuId() <= 0
        ){
            return;
        }

        //出货
        vendingMachineService.vendout(vendoutContract);
    }
}
